/**
 * Created by zjf on 2017/2/22.
 */
//new game ,the game world brief
var GameBeginLayer = cc.Layer.extend({
    currentName:0,
    ctor:function () {
        this._super();
        var self = this;
        //brief cartoon
        //question, influence hero type
        //console.info(g.str1);
        var attr = {
            x:480,
            y:200
        };
        gf.gradualShow(this, g.str1,true,function(){
            setTimeout(gf.gradualShow(self, g.str2,false,function(){
                self.changNameUI();

            },attr),2000)

        });

        //cc.MenuItemFont.setFontName("Times New Roman");
        //cc.MenuItemFont.setFontSize(86);
        //var changeNameItem = new cc.MenuItemFont("重新选择", this.menuItem1Callback, this);
        //var confirmNameBtnItem = new cc.MenuItemFont("22", this.menuItem1Callback, this);
        //var  mn = new cc.Menu(changeNameItem, confirmNameBtnItem);
        //mn.alignItemsVertically();
        //this.addChild(mn);
    },
    changNameUI:function(){
        this.currentName = 0;
        this.yourNameTTF = new cc.LabelTTF(g.names[this.currentName] ,"HanWangShinSuMedium", 38,cc.size(size.width-size.width/4, size.height/2));
        this.yourNameTTF.x = size.width / 2;
        this.yourNameTTF.y = size.height / 4;
        this.addChild(this.yourNameTTF);

        var changeNameBtn = new ccui.Button(res.wrong_png, res.wrong_png);
        changeNameBtn.attr({
            //x:size.height -size.height/3 ,
            //y: size.height / 2,
            x : 500,
            y: 300,
        });
        changeNameBtn.addClickEventListener(this.changeName);
        this.addChild(changeNameBtn);

        var confirmNameBtn = new ccui.Button(res.right_png, res.right_png);
        confirmNameBtn.attr({
            x: 500,
            y: 200,
        });
        confirmNameBtn.addClickEventListener(this.confirmName);
        this.addChild(confirmNameBtn);

    },
    changeName:function(btn){
        var self = btn._parent;
        self.currentName++;
        if(self.currentName >= g.names.length){
            self.currentName = 0;
        }
        self.yourNameTTF.initWithString(g.names[self.currentName],"HanWangShinSuMedium", 38,cc.size(size.width-size.width/4, size.height/2));
    },
    confirmName:function(btn){
        var self = btn._parent;
        g.name = g.names[self.currentName];
        //next layer
            g.multiLayer.switchTo(1);
    }
});



var GameBeginQuestionLayer = cc.Layer.extend({
    ctor:function () {
        this._super();

    },
    onEnter:function () {
        this._super();
        var self = this;

        g.str3 = "原来是"+ g.name+"大侠，久仰久仰";

        var attr = {
            x:480,
            y:400
        };
        gf.gradualShow(this, g.str3,true,function(){
            setTimeout(gf.gradualShow(self, g.str4,false,function(){
                self.changHobbyUI();
            },attr),2000)

        });


    },
    changHobbyUI:function(){
        cc.MenuItemFont.setFontName("Times New Roman");
        cc.MenuItemFont.setFontSize(42);
        var answer1 = new cc.MenuItemFont("a:"+g.answer4_1, this.answer1Callback, this);
        var answer2 = new cc.MenuItemFont("b:"+g.answer4_2, this.answer2Callback, this);
        var answer3 = new cc.MenuItemFont("c:"+g.answer4_3, this.answer3Callback, this);
        var  mn = new cc.Menu(answer1, answer2,answer3);
        mn.attr({
            x:size.width/3,
            y:size.height/3
        });
        //mn.alignItemsVertically();
        mn.alignItemsHorizontally();
        this.addChild(mn);
    },

    answer1Callback:function(menu){
        //console.log(this);
        //console.log(menu);
        //var self = menu._parent;
        //ok,notice self and this .mumuhaha!!!
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 0;
        var attr = {
            x:480,
            y:350
        };
        gf.gradualShow(self, g.str41,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer41_1, self.answer11Callback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer41_2, self.answer12Callback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer41_3, self.answer13Callback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },


    answer2Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 1;
        var attr = {
            x:480,
            y:350
        };
        gf.gradualShow(self, g.str42,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer42_1, self.answer21Callback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer42_2, self.answer22Callback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer42_3, self.answer23Callback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answer3Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 2;
        var attr = {
            x:480,
            y:350
        };
        gf.gradualShow(self, g.str43,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer43_1, self.answer31Callback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer43_2, self.answer32Callback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer43_3, self.answer33Callback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },

    answer11Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 0;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str411,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer411_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer411_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer411_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },

    answer12Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 1;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str412,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer412_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer412_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer412_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);

    },
    answer13Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 2;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str411,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer411_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer411_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer411_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answer21Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 0;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str421,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer421_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer421_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer421_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answer22Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 1;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str422,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer422_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer422_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer422_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answer23Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 2;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str423,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer423_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer423_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer423_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answer31Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 0;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str431,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer431_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer431_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer431_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answer32Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 1;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str432,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer432_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer432_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer432_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answer33Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue += 2;
        var attr = {
            x:480,
            y:300
        };
        gf.gradualShow(self, g.str433,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer433_1, self.answerLastCallback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer433_2, self.answerLastCallback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer433_3, self.answerLastCallback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answerLastCallback:function(menu){
        var self = this;
        self.removeChild(menu._parent);

        //console.log(this);
        //console.log(menu._parent);
        //console.log(menu._label._string);
        var string = menu._label._string.substring(0,1);
        switch (string){
            case "a":
                g.choseValue +=0;
                break;
            case "b":
                g.choseValue +=1;
                break;
            case "c":
                g.choseValue +=2;
                break;
        }
        var attr = {
            x:480,
            y:250
        };
        gf.gradualShow(self, g.str5,false,function(){
            cc.MenuItemFont.setFontName("Times New Roman");
            cc.MenuItemFont.setFontSize(42);
            var answer1 = new cc.MenuItemFont("a:"+g.answer5_1, self.answerLast_1Callback, self);
            var answer2 = new cc.MenuItemFont("b:"+g.answer5_2, self.answerLast_2Callback, self);
            var answer3 = new cc.MenuItemFont("c:"+g.answer5_3, self.answerLast_3Callback, self);
            var  mn = new cc.Menu(answer1, answer2,answer3);
            mn.attr({
                x:size.width/3,
                y:size.height/3
            });
            mn.alignItemsHorizontally();
            self.addChild(mn);
        },attr);
    },
    answerLast_1Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue +=0;
        g.multiLayer.switchTo(2);

    },
    answerLast_2Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue +=1;
        g.multiLayer.switchTo(2);
    },
    answerLast_3Callback:function(menu){
        var self = this;
        self.removeChild(menu._parent);
        g.choseValue +=2;
        g.multiLayer.switchTo(2);
    },
});

var GameBeginBuiltHeroLayer = cc.Layer.extend({
    ctor:function(){
        this._super();
    },
    onEnter:function(){
        if(g.choseValue<=1){
            heroArray[0] = heroArray[2];
            heroArray.length = 1;
        }else if(g.choseValue<=3){
            heroArray[0] = heroArray[3];
            heroArray.length = 1;
        }else if(g.choseValue<=5){
            heroArray[0] = heroArray[1];
            heroArray.length = 1;
        }else{
            heroArray[0] = heroArray[0];
            heroArray.length = 1;
        }

        console.log(heroArray);

        cc.director.runScene(new GameScene());
    }
});


var GameBeginScene = cc.Scene.extend({



    //choseNumber:0,

    ctor:function () {
        this._super();

        cc.audioEngine.playMusic(res.gameBegin_mp3,true);

        var bgLayer = new cc.LayerColor(cc.color(123,123,131, 255), size.width, size.height)
        this.addChild(bgLayer);

        g.multiLayer = new cc.LayerMultiplex(new GameBeginLayer(), new GameBeginQuestionLayer(),new GameBeginBuiltHeroLayer());
        this.addChild(g.multiLayer);
        g.multiLayer.switchTo(0);



    },


    //onEnter:function () {
    //    this._super();
    //
    //    var layer = new GameBeginLayer();
    //    this.addChild(layer);
    //}
});